<template>
    <div class="app-container">
        <div class="filter-container">
            <el-input v-model="listQuery.tableName" size="small" placeholder="请输入表名" clearable class="filter-item" />
            <el-input v-model="listQuery.annotations" size="small" placeholder="请输入表注释" clearable class="filter-item" />
            <el-select v-model="listQuery.module" placeholder="模块名称" clearable size="small">
                <el-option v-for="module in moduleName" :key="module" :label="module" :value="module" />
            </el-select>
            <el-select v-model="listQuery.type" placeholder="类型" clearable size="small" style="margin: 0 10px;">
                <el-option v-for="type in typeName" :key="type" :label="type" :value="type" />
            </el-select>
            <el-select v-model="listQuery.updateFrequency" placeholder="更新方式" clearable size="small"
                style="margin: 0 10px;">
                <el-option v-for="type in frequencyArr" :key="type" :label="type" :value="type" />
            </el-select>

            <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button size="small" icon="el-icon-refresh" @click="refresh">重置</el-button>
        </div>
        <el-table v-loading="listLoading" :data="tableData" element-loading-text="Loading" border fit height="580px"
            class="table-container" highlight-current-row>
            <el-table-column type="index" label="序号" prop width="50"></el-table-column>
            <el-table-column label="模块名称" align="center" prop="module"> </el-table-column>
            <el-table-column label="表格名称" align="center" prop="tableName" width="200"> </el-table-column>
            <el-table-column label="表注释" align="center" prop="annotations" width="250"></el-table-column>
            <el-table-column label="类型" align="center" prop="type"></el-table-column>
            <el-table-column label="更新方式" align="center" prop="updateFrequency"></el-table-column>
            <el-table-column label="总行数" align="center" prop="rowTotal">
            </el-table-column>
            <el-table-column label="操作" align="center" prop="rowTotal" width="200">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="checkDetails(scope.row)">查看表结构</el-button>
                    <el-button v-if="scope.row.taskId" size="mini" type="text"
                        @click="toUpdate(scope.row)">频率操作</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination style="margin-top: 20px" v-show="total > 0" :total="total" :page.sync="listQuery.pageNo"
            :limit.sync="listQuery.pageSize" @pagination="fetchData" />
        <el-dialog title="任务详情" :visible.sync="updateShow" width="30%" :close-on-click-modal="false"
            :before-close="handleClose">
            <div class="update-wrap">
                <div class="taskInfo">
                    <div class="info-item">任务名称:{{ currentTaskObj.DATA_NAME }}</div>
                    <div class="info-item">任务状态: <el-tag :type="currentTaskObj.STATUS == 0 ? 'danger' : 'success'"> {{
                        currentTaskObj.STATUS == 0 ? '暂停' : '运行中'
                            }}</el-tag>
                        <!-- {{ taskStatus  v-model="taskStatus" }}  disabled-->
                        <el-switch class="captcha-img" :value="taskStatus" active-color="#13ce66"
                            inactive-color="#ff4949" style="margin-left: 10px;" active-text="启动" inactive-text="暂停"
                            @click.native="submitTask">
                        </el-switch>
                    </div>
                    <div class="info-item">
                        更新频率:
                        <el-select v-model="currentTaskObj.CRON" :disabled="taskDisabled" style="width: 300px;">
                            <el-option v-for="item in cronSelectList" :key="item.CRON" :label="item.COMPARE"
                                :value="item.CRON"></el-option>
                        </el-select>
                        <img v-if="taskDisabled" style="cursor: pointer;" src="@/assets/icon/edit.png" alt=""
                            class="edit" @click="taskDisabled = false" />
                    </div>
                </div>

                <div class="btn-group" v-if="!taskDisabled" style="margin-bottom: 10px;">
                    <el-button @click="taskDisabled = true">取消</el-button>
                    <el-button type="primary" @click="updateFrequency">保存当前设置</el-button>
                </div>
                <!-- :rules="rules" -->
                <!-- <el-form :model="currentTaskObj" ref="ruleForm" class="demo-ruleForm" label-position="left">
                    <el-form-item label="更新频率" prop="name">

                    </el-form-item>

                    <div class="text-right">
                        <el-button type="danger" @click="updateShow = false">取消</el-button>
                        <el-button type="primary" @click="submitTask">{{ currentTaskObj.STATUS == 1 ? '暂停'
                            : '运行' }}</el-button>
                    </div>
                </el-form> -->
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { tableManagerList, cronList, taskInfoById, taskPause, taskResume, taskUpdate } from '@/http/api/myHttp.js'
import { formatX } from "@/utils/fontSize"
export default {
    name: "ThemeSearch",
    components: {},
    data() {
        return {
            ruleForm: {},
            total: 0,
            tableData: [],
            listLoading: false,
            listQuery: {
                pageNo: 1,
                pageSize: 10,
                module: "",
                type: "",
                tableName: "",
                annotations: ""
            },
            moduleName: ["智慧林业", "环境卫生", "公园绿地", "综合评价"],
            typeName: ["物联感知数据", "结果数据", "基础数据", "市级数据", "区级数据", "业务数据", "视频数据"],
            frequencyArr: ['手动更新', '库表定时更新', '定时更新', '实时推送'],
            updateShow: false,
            currentRow: {},
            rules: {
                name: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ]
            },
            taskDisabled: true,
            cronSelectList: [],
            currentTaskObj: {},
            taskStatus: false
        }
    },
    created() {
        this.fetchData()
        this.getCronList()
    },
    watch: {
        $route(to, from) {
            // 判断是否是主题库列表和主题库详情回来的。
            if (!['TableDetail','ThemeSearch'].includes(from.name)) {
                this.refresh()
            }
        }
    },
    methods: {
        handleClose() {
            this.taskDisabled = true
            this.updateShow = false
        },
        getCronList() {
            cronList().then(res => {
                if (res.code == 0) {
                    this.cronSelectList = res.data
                }
            })
        },
        toUpdate(row) {
            taskInfoById({ taskId: row.taskId }).then(res => {
                if (res.code == 0) {
                    this.currentRow = row
                    this.updateShow = true
                    this.currentTaskObj = res.data
                    this.taskStatus = res.data.STATUS == 1 ? true : false
                }
            })
        },
        checkDetails(row) {
            this.$router.push({
                path: '/themeTable/tableDetail',
                query: {
                    tableName: row.tableName
                }
            })
        },
        formatX,
        search() {
            this.listQuery.pageNo = 1
            this.fetchData()
        },
        refresh() {
            this.listQuery = {
                pageNo: 1,
                pageSize: 10,
                date: ""
            }
            this.fetchData()
        },
        fetchData() {
            let params = { ...this.listQuery }
            for (let key in params) {
                if (!params[key]) {
                    delete params[key]
                }
            }
            this.listLoading = true
            tableManagerList(params).then((res) => {
                this.listLoading = false
                if (res.code == 0) {
                    this.tableData = res.data.records

                    this.total = res.data.total
                }
            })
        },
        updateFrequency() {
            this.$confirm(`确认要修改任务的更新频率吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    var obj = {
                        objectId: this.currentTaskObj.OBJECT_ID,
                        cron: this.currentTaskObj.CRON
                    }
                    console.log(obj)
                    const loading = this.$loading({
                        lock: true,
                        text: '正在修改...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.5)'
                    })
                    taskUpdate(obj).then((res) => {
                        if (res.code == 0) {
                            taskInfoById({ taskId: this.currentTaskObj.OBJECT_ID }).then(taskRes => {
                                if (taskRes.code == 0) {
                                    loading.close()
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    })
                                    this.currentTaskObj = taskRes.data
                                    this.taskStatus = taskRes.data.STATUS == 1 ? true : false
                                    this.taskDisabled = true
                                }
                            })
                        }
                    })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    })
                })
        },
        submitTask() {
            // 要执行的请求
            let resFnc = this.currentTaskObj.STATUS == 1 ? taskPause : taskResume
            let confirmText = this.currentTaskObj.STATUS == 1 ? '确认暂停该定时任务吗？' : '确认运行该定时任务吗？'
            this.$confirm(confirmText, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                resFnc({ objectId: this.currentTaskObj.OBJECT_ID }).then(res => {
                    if (res.code == 0) {
                        const loading = this.$loading({
                            lock: true,
                            text: '正在修改...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.5)'
                        })
                        taskInfoById({ taskId: this.currentTaskObj.OBJECT_ID }).then(taskRes => {
                            if (taskRes.code == 0) {
                                loading.close()
                                this.$message({
                                    message: this.currentTaskObj.STATUS == 1 ? '暂停成功' : '运行成功',
                                    type: 'success'
                                })
                                this.taskStatus = taskRes.data.STATUS == 1 ? true : false
                                this.currentTaskObj = taskRes.data
                            }
                        })
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.app-container {
    // box-sizing: border-box;
    width: 100%;
    height: 100%;
    // padding: 18px 24px;
}

.taskInfo {
    .info-item {
        margin: 10px 0;
        display: flex;
        align-items: center;
        justify-content: flex-start;
    }
}

.filter-container {
    display: flex;
    justify-content: flex-start;
}

.filter-item {
    width: 300px;
    margin-right: 10px;
}

.el-table {
    ::v-deep {
        margin-top: 20px;

        .el-table__header {
            border-radius: 6px;
            overflow: hidden;
        }

        .el-table__cell {
            height: 50px;
            padding: 0;
        }

        .is-leaf {
            background-image: linear-gradient(to top, rgba(128, 191, 255, 1), rgba(51, 153, 255, 1));
            font-size: 14px;
            font-weight: 700;
            letter-spacing: 0px;
            line-height: 24px;
            color: rgba(255, 255, 255, 1);
            border-right: 1px solid white;
        }
    }
}

.operate_area {
    width: 80px;
    display: flex;
    justify-content: space-between;
    margin: 0 auto;

    img {
        cursor: pointer;
    }
}

.captcha-img.el-switch.is-disabled {
    opacity: 1;

}

.update-wrap {}
</style>
<style>
.el-switch__core {
    cursor: pointer;
}
</style>
